package org.luckystrophe.snowtoms.model;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import org.luckystrophe.snowtoms.manager.HttpConnectManager;
import org.luckystrophe.snowtoms.manager.Md5Maker;

import android.util.Log;

public abstract class JsonRequestPOST implements JsonRequest {

//	public static final String REMOTE_SNOWTOMS = "http://220.76.202.96/api";
//
//	public static final String REMOTE_SNOWTOMS_IMAGE = "http://220.76.202.96:8003";

	public static final String REMOTE_SNOWTOMS = "http://snowtoms.cafe24.com/api";

	public static final String REMOTE_SNOWTOMS_IMAGE = "http://snowtoms.cafe24.com:8003";

	public static final int NOT_FOUND_METHOD = -10;

	public static final HttpPost SNOWTOMS_POST_METHOD = new HttpPost(REMOTE_SNOWTOMS);

	public HttpUriRequest getHttpUriRequest() throws UnsupportedEncodingException, JSONException {
		HttpPost httpPost = SNOWTOMS_POST_METHOD;
		long ts = System.currentTimeMillis();
		JSONObject body = getBody().put("fingerPrint", Md5Maker.encryptString("snowtoms", ts)).put("timeStamp", ts)
				.put("fpKey", "snowtoms");
		StringEntity se = new StringEntity(body.toString(), HTTP.UTF_8);
		httpPost.setEntity(se);
		httpPost.setHeader("Content-type", "application/json");
		return httpPost;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.luckystrophe.snowtoms.model.JsonRequest#sendJsonRequest() Result
	 * code detail
	 */

	public JSONObject sendJsonRequest() throws ClientProtocolException, JSONException, IOException {
		JSONObject resultJo = HttpConnectManager.getConnection(this);

		int resultCode = resultJo.getInt("resultCode");
		if (resultCode == 1) {
			// return HttpConnectManager.getConnection(this);
		} else {
			Log.e("Mas", "ERROR : " + resultCode);
		}
		return resultJo;
	}

	public abstract JSONObject getBody() throws JSONException;

}
